/**
 * Created by Administrator on 2017/5/3.
 */

var redis_db = require('../config/redis2Qrcode');
var hmCodeDao = module.exports;

hmCodeDao.save = function (data, cb) {
    if(!data) {
        cb(new Error(500));
    } else {
        redis_db.get(data.uid, function(err, result) {
            if(err) {
                console.log(err);
                cb(false);
            } else {
                // t  被扫描的总数    c  避免hid重名    p  避免qrid重名
                if(data.hid == undefined) {
                    if(result) {
                        var hmInfo = JSON.parse(result);
                        hmInfo.c = parseInt(hmInfo.c) + 1;
                        hmInfo[hmInfo.c] = {};
                        hmInfo[hmInfo.c].hmName = data.hmName;
                        hmInfo[hmInfo.c].threshold = data.threshold;
                        hmInfo[hmInfo.c].curQid = 0;
                        hmInfo[hmInfo.c].t = 0;
                        hmInfo[hmInfo.c].p = 0;
                        hmInfo[hmInfo.c].qrs = {};
                    } else {
                        var hmInfo = {};
                        hmInfo.c = 1;
                        hmInfo[hmInfo.c] = {};
                        hmInfo[hmInfo.c].hmName = data.hmName;
                        hmInfo[hmInfo.c].threshold = data.threshold;
                        hmInfo[hmInfo.c].curQid = 0;
                        hmInfo[hmInfo.c].t = 0;
                        hmInfo[hmInfo.c].p = 0;
                        hmInfo[hmInfo.c].qrs = {};
                    }
                    redis_db.set(data.uid,JSON.stringify(hmInfo),'', function(e){
                        if(e) {
                            console.log(e);
                            cb(false);
                        }
                        var info = {};
                        info.url = 'hm?'+ data.uid +'_'+ hmInfo.c;
                        info.hid = hmInfo.c;
                        info.hmName = data.hmName;
                        info.threshold = data.threshold;
                        cb(null,info);
                        return;
                    });
                } else {
                    if(result) {
                        var hmInfo = JSON.parse(result);
                        hmInfo[data.hid].hmName = data.hmName;
                        hmInfo[data.hid].threshold = data.threshold;
                        redis_db.set(data.uid,JSON.stringify(hmInfo),'', function(e){
                            if(e) {
                                console.log(e);
                                cb(false);
                            }
                            cb(null);
                        });
                    } else {
                        cb(false);
                    }
                }
            }
        });
    }
}

hmCodeDao.addQrcode = function(data, cb) {
    redis_db.get(data.uid, function(err, result) {
        if(err) {
            console.log(err);
            cb(false);
        } else {
            if(result) {
                var hmInfo = JSON.parse(result);

                if(hmInfo[data.hid] == undefined) {
                    cb(false);
                } else {
                    hmInfo[data.hid].threshold = data.threshold;
                    hmInfo[data.hid].hmName = data.hmName;

                    if(hmInfo[data.hid].qrs[hmInfo[data.hid].p] == undefined) {
                        hmInfo[data.hid].qrs[hmInfo[data.hid].p] = {};
                        hmInfo[data.hid].qrs[hmInfo[data.hid].p].c = 0;
                        hmInfo[data.hid].qrs[hmInfo[data.hid].p].picUrl = data.picUrl +'/'+ data.picName;
                    } else {
                        hmInfo[data.hid].p = parseInt(hmInfo[data.hid].p) + 1;
                        hmInfo[data.hid].qrs[hmInfo[data.hid].p] = {};
                        hmInfo[data.hid].qrs[hmInfo[data.hid].p].c = 0;
                        hmInfo[data.hid].qrs[hmInfo[data.hid].p].picUrl = data.picUrl +'/'+ data.picName;
                    }
                    redis_db.set(data.uid,JSON.stringify(hmInfo),'', function(e){
                        if(e) {
                            console.log(e);
                            cb(false);
                        }
                        cb(null,'上传二维码成功！');
                    });
                }
            } else {
                cb(false);
            }
        }
    });
}

hmCodeDao.getPicName = function(uid, hid, cb) {
    redis_db.get(uid, function(err, result) {
        if(err) {
            console.log(err);
            cb(false);
        } else {
            if(result) {
                var hmInfo = JSON.parse(result);
                var picName = (parseInt(hmInfo[hid].p) + 1) +'.jpg';
                hmInfo[hid].p += 1;
                redis_db.set(uid, JSON.stringify(hmInfo), '', function(e) {
                    if(e) {
                        cb(false);
                    } else {
                        cb(null, picName);
                    }
                })
            }
        }
    });
}

hmCodeDao.getQrcodes = function(data, cb) {
    if(!data) {
        cb(new Error(500));
    } else {
        redis_db.get(data.uid, function(err, result) {
            if(err) {
                console.log(err);
                cb(false);
            } else {
                if(result) {
                    var qrCodes = JSON.parse(result);
                    cb(null, qrCodes[data.hid]);
                } else {
                    cb(null);
                }
            }
        });
    }
}

hmCodeDao.delHmCodes = function (data, cb) {
    if(!data) {
        cb(new Error(500));
    } else {
        redis_db.get(data.uid, function(err, result) {
            if(err) {
                console.log(err);
                cb(false);
            } else {
                if(result) {
                    var qrCodes = JSON.parse(result);
                    delete qrCodes[data.hid];
                    redis_db.set(data.uid, JSON.stringify(qrCodes), '', function(e){
                        if(e) {
                            console.log(e);
                            cb('删除活码失败！');
                        }
                        cb(null, '删除成功！');
                    });
                    //增加数据库记录  记录删除时间  哪类活码
                }
            }
        });
    }
}

hmCodeDao.delQrCode = function (data, cb) {
    if(!data) {
        cb(new Error(500));
    } else {
        redis_db.get(data.uid, function(err, result) {
            if(err) {
                console.log(err);
                cb(false);
            } else {
                if(result) {
                    var qrCodes = JSON.parse(result);
                    delete qrCodes[data.hid].qrs[data.qrId];
                    redis_db.set(data.uid, JSON.stringify(qrCodes), '', function(e){
                        if(e) {
                            console.log(e);
                            cb('删除二维码失败！');
                        }
                        cb(null, '删除成功！');
                    });
                    //增加数据库记录  记录删除时间  哪张qrcode
                }
            }
        });
    }
}

hmCodeDao.getHmCodes = function(data, cb) {
    if(!data) {
        cb(new Error(500));
    } else {
        redis_db.get(data.uid, function(err, result) {
            if(err) {
                console.log(err);
                cb(false);
            } else {
                var hmCodes = JSON.parse(result);
                if(hmCodes == undefined) {
                    cb(null, []);
                } else {
                    hmInfo = [];
                    for(var i=1; i<=hmCodes.c; i++) {
                        if(hmCodes[i] != undefined) {
                            var arr = {};
                            arr.hmName = hmCodes[i].hmName;
                            arr.threshold = hmCodes[i].threshold;
                            arr.totalNumber = hmCodes[i].t;
                            arr.hid = i;
                            var count = 0;
                            for(var k in hmCodes[i].qrs) {
                                count++;  //共有几张
                            }
                            arr.count = count;

                            for(var j=0;j<=hmCodes[i].p; j++) {
                                if(hmCodes[i].qrs[j] != undefined) {
                                    if(hmCodes[i].qrs[hmCodes[i].curQid] != undefined && hmCodes[i].qrs[hmCodes[i].curQid].c < hmCodes[i].threshold) {
                                        arr.picUrl = hmCodes[i].qrs[hmCodes[i].curQid].picUrl;
                                        break;
                                    }
                                    else if(hmCodes[i].qrs[j].c != undefined && hmCodes[i].qrs[j].c < hmCodes[i].threshold) {
                                        arr.picUrl = hmCodes[i].qrs[j].picUrl;
                                        break
                                    }
                                }
                            }
                            hmInfo.push(arr);
                        }
                    }
                    cb(null, hmInfo);
                }
            }
        });
    }
}

hmCodeDao.changeScanTimes = function (data, cb ) {
    if(!data) {
        cb(new Error(500));
    } else {
        redis_db.get(data.uid, function(err, result) {
            if(err) {
                console.log(err);
                cb(false);
            } else {
                var hmInfo = JSON.parse(result);
                if(hmInfo) {

                    if(hmInfo[data.hid]) {
                        hmInfo[data.hid].qrs[data.qrId].c = parseInt(data.scanTimes);
                        var sum = 0;
                        for(var i=0;i<=hmInfo[data.hid].p; i++) {
                            if(hmInfo[data.hid].qrs[i] != undefined) {
                                sum +=  parseInt(hmInfo[data.hid].qrs[i].c);
                                //console.log(hmInfo[data.hid].t);
                            }
                        }
                        hmInfo[data.hid].t = sum;
                        redis_db.set(data.uid, JSON.stringify(hmInfo), '', function(e){
                            if(e) {
                                console.log(e);
                                cb(false);
                            }
                            cb(null, '修改成功！');
                        });
                    } else {
                        cb(false);
                    }
                } else {
                    cb(false);
                }
            }
        });
    }
}

function getCount(uid, cb) {
    redis_db.get(uid, function(err, result) {
        if(err) {
            console.log(err);
            cb(false);
        } else {
            var hmInfo = JSON.parse(result);
            if(hmInfo) {
                var len = hmInfo.c;
                var count = 0;
                if(len != undefined) {
                    for(var i=0; i<=len; i++) {
                        if(hmInfo[i] != undefined) {
                            count++;
                        }
                    }
                }
                cb(null, count);
            } else {
                cb(null, 0);
            }
        }
    });
}

hmCodeDao.canCreate = function(data, cb) {
    var canCreate = 0;
    for(var i=0; i<data.appInfo.length; i++) {
        if(data.appInfo[i].app_sid == 'huoma' ) {
           if( data.appInfo[i].vipInfo['huomaAmount']['can_use'] == 1 ) {
               canCreate = data.appInfo[i].vipInfo['huomaAmount']['vip_level'];
           } else {
               cb('请先购买会员！');
               return;
           }
           break;
        }
    }
    getCount(data.uid, function(e, count){
       if(e) {
           console.log('getCount err' + e);
           cb('获取活码张数发生错误请重试！');
       } else {
           if(canCreate > count) {
               cb(null, true);
           } else {
               cb('您创建的活码张数已达到上限，请购买更高类型会员！');
           }
       }
    });
}

hmCodeDao.canUse = function(data, cb) {
    var canCreate = 0;
    for(var i=0; i<data.appInfo.length; i++) {
        if(data.appInfo[i].app_sid == 'huoma' ) {
            if( data.appInfo[i].vipInfo['huomaAmount']['can_use'] == 1 ) {
                canCreate = data.appInfo[i].vipInfo['huomaAmount']['vip_level'];
            } else {
                cb('请先购买会员！');
                return;
            }
            break;
        }
    }
    getCount(data.uid, function(e, count){
        if(e) {
            console.log('getCount err' + e);
            cb('获取活码张数发生错误请重试！');
        } else {
            if(canCreate < count) {
                cb('请先删除多余的活码，再编辑，或者购买更高等级会员');
            } else {
                cb(null, true);
            }
        }
    });
}